package com.example.pdftomarkdown.config;

import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.unit.DataSize;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.MultipartConfigElement;
import java.util.HashMap;
import java.util.Map;

/**
 * Web配置类
 * 配置文件上传限制和其他Web相关设置
 * 注意：字符编码由Spring Boot自动配置，无需手动配置
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    /**
     * 配置文件上传限制
     * 
     * @return MultipartConfigElement 文件上传配置
     */
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        
        // 单个文件最大大小：10MB
        factory.setMaxFileSize(DataSize.ofMegabytes(10));
        
        // 总上传数据最大大小：10MB
        factory.setMaxRequestSize(DataSize.ofMegabytes(10));
        
        return factory.createMultipartConfig();
    }
}

/**
 * 全局异常处理器
 * 处理应用程序中的各种异常情况
 */
@RestControllerAdvice
class GlobalExceptionHandler {

    /**
     * 处理文件上传大小超限异常
     * 
     * @param e 异常信息
     * @return 错误响应
     */
    @ExceptionHandler({MaxUploadSizeExceededException.class, MultipartException.class})
    public ResponseEntity<Map<String, Object>> handleFileUploadException(Exception e) {
        Map<String, Object> error = new HashMap<>();
        error.put("success", false);
        error.put("message", "文件大小超出限制，请选择小于10MB的PDF文件");
        error.put("error", "FILE_TOO_LARGE");
        error.put("maxSize", "10MB");
        
        return ResponseEntity.status(HttpStatus.PAYLOAD_TOO_LARGE).body(error);
    }

    /**
     * 处理非法参数异常
     * 
     * @param e 异常信息
     * @return 错误响应
     */
    @ExceptionHandler(IllegalArgumentException.class)
    public ResponseEntity<Map<String, Object>> handleIllegalArgumentException(IllegalArgumentException e) {
        Map<String, Object> error = new HashMap<>();
        error.put("success", false);
        error.put("message", e.getMessage());
        error.put("error", "INVALID_ARGUMENT");
        
        return ResponseEntity.badRequest().body(error);
    }

    /**
     * 处理IO异常
     * 
     * @param e 异常信息
     * @return 错误响应
     */
    @ExceptionHandler(java.io.IOException.class)
    public ResponseEntity<Map<String, Object>> handleIOException(java.io.IOException e) {
        Map<String, Object> error = new HashMap<>();
        error.put("success", false);
        error.put("message", "文件处理失败: " + e.getMessage());
        error.put("error", "FILE_PROCESSING_ERROR");
        
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error);
    }

    /**
     * 处理其他未预期的异常
     * 
     * @param e 异常信息
     * @return 错误响应
     */
    @ExceptionHandler(Exception.class)
    public ResponseEntity<Map<String, Object>> handleGenericException(Exception e) {
        Map<String, Object> error = new HashMap<>();
        error.put("success", false);
        error.put("message", "服务器内部错误，请稍后重试");
        error.put("error", "INTERNAL_SERVER_ERROR");
        error.put("details", e.getMessage());
        
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error);
    }
}